
 // 获取元素
 const taskInput = document.getElementById('taskInput');
 const addTaskButton = document.getElementById('addTaskButton');
 const prioritySelect = document.getElementById('prioritySelect');
 const reminderInput = document.getElementById('reminderInput');
 const setReminderButton = document.getElementById('setReminderButton');
 const searchInput = document.getElementById('searchInput');
 const sortSelect = document.getElementById('sortSelect');
 const taskList = document.getElementById('taskList');
 const changeThemeButton = document.getElementById('changeThemeButton');
 const totalTasksCount = document.getElementById('totalTasksCount');
 const completedTasksCount = document.getElementById('completedTasksCount');
 const incompleteTasksCount = document.getElementById('incompleteTasksCount');

 // 任务数组，用于存储任务数据
 let tasks = [];
 let isDarkTheme = false;
 let reminderIntervals = {}; // 存储每个任务的定时器 ID

 // 加载本地存储的任务
 const loadTasks = () => {
   const storedTasks = localStorage.getItem('tasks');
   if (storedTasks) {
     tasks = JSON.parse(storedTasks);
     tasks.forEach((task, index) => {
       if (task.dueDate) {
         startCountdown(index, task.dueDate);
       }
     });
     renderTasks();
     updateTaskStatistics();
   }
 };

 // 保存任务到本地存储
 const saveTasks = () => {
   localStorage.setItem('tasks', JSON.stringify(tasks));
 };

// 添加任务
const addTask = () => {
  const taskName = taskInput.value.trim();
  if (taskName === '') return;
  const priority = prioritySelect.value;
  const customDateTimePicker = document.createElement('div');
  customDateTimePicker.id = 'customDateTimePicker';
  customDateTimePicker.style.display = 'none';
  const yearSelect = document.createElement('select');
  const monthSelect = document.createElement('select');
  const daySelect = document.createElement('select');
  const hourSelect = document.createElement('select');
  const minuteSelect = document.createElement('select');
  customDateTimePicker.appendChild(yearSelect);
  customDateTimePicker.appendChild(monthSelect);
  customDateTimePicker.appendChild(daySelect);
  customDateTimePicker.appendChild(hourSelect);
  customDateTimePicker.appendChild(minuteSelect);
  // 填充年份选项（这里简单示例为当前年份前后10年）
  const currentYear = new Date().getFullYear();
  for (let i = currentYear - 10; i <= currentYear + 10; i++) {
      const option = document.createElement('option');
      option.value = i;
      option.textContent = i;
      yearSelect.appendChild(option);
  }
  // 填充月份选项
  for (let i = 1; i <= 12; i++) {
      const option = document.createElement('option');
      option.value = i < 10? '0' + i : i;
      option.textContent = i < 10? '0' + i : i;
      monthSelect.appendChild(option);
  }
  // 填充日期选项（这里简单示例，可根据选择的月份进一步优化闰年等情况）
  for (let i = 1; i <= 31; i++) {
      const option = document.createElement('option');
      option.value = i < 10? '0' + i : i;
      option.textContent = i < 10? '0' + i : i;
      daySelect.appendChild(option);
  }
  // 填充小时选项
  for (let i = 0; i <= 23; i++) {
      const option = document.createElement('option');
      option.value = i < 10? '0' + i : i;
      option.textContent = i < 10? '0' + i : i;
      hourSelect.appendChild(option);
  }
  // 填充分钟选项
  for (let i = 0; i <= 59; i++) {
      const option = document.createElement('option');
      option.value = i < 10? '0' + i : i;
      option.textContent = i < 10? '0' + i : i;
      minuteSelect.appendChild(option);
  }
  const confirmButton = document.createElement('button');
  confirmButton.textContent = '确定';
  confirmButton.addEventListener('click', () => {
      const year = yearSelect.value;
      const month = monthSelect.value;
      const day = daySelect.value;
      const hour = hourSelect.value;
      const minute = minuteSelect.value;
      const dueDate = `${year}-${month}-${day}T${hour}:${minute}`;
      const newTask = { name: taskName, priority, completed: false, dueDate };
      tasks.push(newTask);
      saveTasks();
      taskInput.value = '';
      customDateTimePicker.style.display = 'none';
      renderTasks();
      updateTaskStatistics();
      if (dueDate) {
          const index = tasks.length - 1;
          startCountdown(index, newTask.dueDate);
      }
  });
  customDateTimePicker.appendChild(confirmButton);
  const datetimePickerPlaceholder = document.getElementById('datetimePickerPlaceholder');
  datetimePickerPlaceholder.appendChild(customDateTimePicker);
  customDateTimePicker.style.display = 'block';
};

 // 删除任务
 const deleteTask = (index) => {
   if (reminderIntervals[index]) {
     clearInterval(reminderIntervals[index]);
     delete reminderIntervals[index];
   }
   tasks.splice(index, 1);
   saveTasks();
   renderTasks();
   updateTaskStatistics();
 };

 // 切换任务状态
 const toggleTaskCompletion = (index) => {
   tasks[index].completed =!tasks[index].completed;
   saveTasks();
   renderTasks();
   updateTaskStatistics();
 };

 // 编辑任务
 const editTask = (index) => {
   const newName = prompt('编辑任务名称：', tasks[index].name);
   if (newName!== null && newName.trim()!== '') {
     tasks[index].name = newName;
     saveTasks();
     renderTasks();
   }
 };